/*
 * CS_REQ_INVEN_USE.cpp
 *
 *  Created on: 2013. 10. 21.
 *      Author: jjaehuny
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/Space.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void SendFailInvenUse(User * pUser, int res, int bag_type, int64_t item_seq)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(res);
	* pAck << static_cast<int8_t>(bag_type);
	* pAck << static_cast<int64_t>(item_seq);
	pAck->MakePacket(CMD_SC_REQ_INVEN_USE);
	pUser->Send(pAck);
}

void Command::CS_REQ_INVEN_USE(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * // 조건 검색후 사용 요청해야 함
	CS_REQ_INVEN_USE 	0x1602			///아이템 사용..
	{
		u8 	u8bag_type;			// 가방타입
		u64 	item_seq;			/// 아이템 시퀀스
	}

	SC_REQ_INVEN_USE 	0x1603			///아이템 사용..
	{
		s16	ack;					// 301
		u8 	u8bag_type;			// 가방타입
		u64 item_seq;
	}
	 */

	PREPARE_PACKET;

	uint8_t u8bag_type = 0;
	uint64_t item_seq = 0;

	* pPacket >> u8bag_type >> item_seq;

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(u8bag_type));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_INVEN_USE NULL == pBag u8bag_type : %d", u8bag_type);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(item_seq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_INVEN_USE NULL == pItem : %64d", item_seq);
		return;
	}

	// 사용가능한 아이템이 아닌 경우
	if (1 != pItem->GetData().item_type)
	{
		SendFailInvenUse(pUser, ACK_UNKNOWN_ERROR, 0, 0);
		return;
	}

	// 아이템의 수량이 없는 경우
	if (pItem->GetData().quantity <= 0)
	{
		SendFailInvenUse(pUser, ACK_UNKNOWN_ERROR, 0, 0);
		return;
	}

	int subType = NResource::Static::InstancePtr()->GetItemResource()->GetItemSubType(pItem->GetData());

	// 스탯 초기화 마석
	if (subType == ITEM_SUBTYPE_RETURN_STATPOINT)
	{
		for (int i = 0; i < 4; i++)
		{
			int defaultStat = NLogic::NFunction::GetDefaultStat(pUser->GetCharacterInfo().classtype, static_cast<ESTATNAME>(i));
			int statUpConst = NLogic::NFunction::GetStatUpConst(pUser->GetCharacterInfo().classtype, static_cast<ESTATNAME>(i));

			if (0 >= defaultStat || 0 >= statUpConst)
			{
				SendFailInvenUse(pUser, ACK_UNKNOWN_ERROR, u8bag_type, item_seq);
				return;
			}

			switch (static_cast<ESTATNAME>(i))
			{
			case STAT_STR :
				pUser->GetCharacterInfo().str = defaultStat + ( (pUser->GetCharacterInfo().level - 1) / statUpConst);
				break;

			case STAT_DEX :
				pUser->GetCharacterInfo().dex = defaultStat + ( (pUser->GetCharacterInfo().level - 1) / statUpConst);
				break;

			case STAT_CON :
				pUser->GetCharacterInfo().con = defaultStat + ( (pUser->GetCharacterInfo().level - 1) / statUpConst);
				break;

			case STAT_SPI :
				pUser->GetCharacterInfo().spi = defaultStat + ( (pUser->GetCharacterInfo().level - 1) / statUpConst);
				break;
			}
		}

		pUser->GetCharacterInfo().remain_stat_point	= (pUser->GetCharacterInfo().level - 1) * 3;
	}
	// 스킬 초기화 마석
	else if (subType == ITEM_SUBTYPE_RETURN_SKILLPOINT)
	{
		for (int i = 0; i < 20; i++)
		{
			pUser->GetCharacterInfo().array_Skill[i] = 0;
		}

		// 퀵슬롯 초기화
		for (int i = 0; i < 8; i++)
		{
			// 퀵슬롯에 등록된 아이템은 초기화 하지 않는다
			if (1 == pUser->GetCharacterInfo().array_QuickSlot[i].type)
				continue;

			pUser->GetCharacterInfo().array_QuickSlot[i].type	= 0;
			pUser->GetCharacterInfo().array_QuickSlot[i].index	= 0;
		}

		// 스킬포인트는 60레벨까지만 지급하므로, 61레벨이상인 경우는 59로..
		if (61 <= pUser->GetCharacterInfo().level)
			pUser->GetCharacterInfo().remain_skill_point = 59;
		else
			pUser->GetCharacterInfo().remain_skill_point = pUser->GetCharacterInfo().level - 1 ;


		pUser->FlushCharacterInfoCache();
	}
	else if (subType == ITEM_SUBTYPE_POTION)
	{
		NLogic::Space * pSpace = pUser->GetSpace();
		if (NULL == pSpace)
			return;

		if (502 == pUser->GetVersion() && pItem->GetData().quantity <= 1 && pSpace->GetData()._spaceType == NLogic::Space::SpaceType::SINGLE_DUNGEION)
		{
			std::string emergency = " !cFFFF00물약이 하나 남았을 때는 사용할 수 없어요.";

			::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
			*pNoti << emergency.c_str();
			pNoti->MakePacket(CMD_SC_EMERGENCY_NOTICE);
			pUser->Send(pNoti);
			return;
		}
	}

	// 아이템 보유 수량 차감
	pItem->GetData().quantity -= 1;

	if (pItem->GetData().quantity <= 0)
	{
		pBag->PopItem(item_seq);
		pItem->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_ITEM_USE);
	}
	else
	{
		pItem->UpdateDatabaseItemInfo();
	}

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	* pAck << u8bag_type;
	* pAck << item_seq;
	pAck->MakePacket(CMD_SC_REQ_INVEN_USE);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/


